package com.soul.server.server;

import com.soul.commons.constans.Constants;
import com.soul.commons.exception.SoulGlobalException;
import com.soul.dubbo.api.db.UserInfoApi;
import com.soul.dubbo.api.mongo.CommentApi;
import com.soul.dubbo.api.mongo.MovementApi;
import com.soul.model.db.UserInfo;
import com.soul.model.eums.CommentType;
import com.soul.model.mongo.Comment;
import com.soul.model.mongo.Movement;
import com.soul.model.mongo.Praise;
import com.soul.model.vo.CommentVo;
import com.soul.model.vo.PageResult;
import com.soul.server.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Service
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 圈子 - 评论列表
     *
     * @param movementId 动态id
     * @param page       每页码数
     * @param pagesize   每页条数
     * @return PageResult<Movement>
     */
    public PageResult<CommentVo> findComment(String movementId, Long page, Long pagesize) {

        // 1. 查询comment集合的评论列表
        PageResult<Comment> commentPageResult = commentApi.findPageComment(movementId, page, pagesize);

        if (CollectionUtils.isEmpty(commentPageResult.getItems())) {
            return new PageResult<>(page, pagesize, 0l, null);
        }
        // 2.拿到动态用户详细信息
        List<Comment> commentList = commentPageResult.getItems();
        List<Long> commentIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(commentIds);

        // 3. 构建数据封装
        List<CommentVo> commentVoList = new ArrayList<>();
        if (userInfoMap != null) {
            for (Comment comment : commentList) {
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                if (userInfo != null) {
                    commentVoList.add(CommentVo.init(userInfo, comment));
                }

            }

        }
        return new PageResult<>(page, pagesize, commentPageResult.getCounts(), commentVoList);
    }

    /**
     * 圈子 - 新增评论
     *
     * @param movementId 动态id
     * @param comment    评论内容
     */
    public void addComment(String movementId, String comment) {
        Comment c = new Comment();
        c.setCommentType(CommentType.COMMENT.getType());
        c.setPublishId(new ObjectId(movementId));
        c.setContent(comment);
        c.setUserId(UserHolder.getUserId());
        commentApi.addComment(c);
    }

    /**
     * 评论-点赞
     *
     * @param commentId 评论id
     * @return 点赞数
     */
    public Integer commentLike(String commentId) {
        Long currendUserId = UserHolder.getUserId();
        Praise praise = new Praise();
        praise.setUserId(currendUserId);
        praise.setPublishId(new ObjectId(commentId)); // 评论动态id
        praise.setCommentType(CommentType.LIKE.getType()); // 评论类型：1-点赞，2-评论，3-喜欢
        Integer count = commentApi.commentLike(praise); // 评论点赞
        String key = "COMMENTLIKE_" + CommentType.LIKE.getType();
        String hashKey = "COMMENTLIKE_" + currendUserId;
        stringRedisTemplate.opsForHash().put(key,hashKey,"1");
        return count;
    }

    /**
     * 评论-取消点赞
     *
     * @param commentId 评论id
     * @return 点赞数
     */
    public Integer commentDislike(String commentId) {
        Long currendUserId = UserHolder.getUserId();
        Praise praise = new Praise();
        praise.setUserId(currendUserId);
        praise.setPublishId(new ObjectId(commentId)); // 评论动态id
        praise.setCommentType(CommentType.LIKE.getType()); // 评论类型：1-点赞，2-评论，3-喜欢
        Integer count = commentApi.commentDislike(praise); // 评论取消点赞
        String key = "COMMENTLIKE_" + CommentType.LIKE.getType();
        String hashKey = "COMMENTLIKE_" + currendUserId;
        stringRedisTemplate.opsForHash().delete(key,hashKey);
        return count;
    }
}
